﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.Area;
using LJX.SmartPark.Domain.IotInfo;
using LJX.SmartPark.Service.IotInfo;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LJX.SmartPark.Service.Area.SchedulSer.Dto;

namespace LJX.SmartPark.Service.Area
{
    public class CarService : ICarService
    {
        private readonly IRepository<Car> _studentService;

        public CarService(IRepository<Car> studentService)
        {
            _studentService = studentService;
        }

        public async Task<Response<bool>> DeleteAllAsync(params Car[] input)
        {
            var result = await _studentService.DeleteAsync(input);
            return Response<bool>.Ok(result > 0);
        }

        public async Task<Response<bool>> DeleteByIdAsync(Car input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<Car>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 显示分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<List<Car>>> GetCarAsync(CarForm input)
        {
            var obj = _studentService.GetListQuery();
            obj = obj.WhereIf(!string.IsNullOrWhiteSpace(input.brandModel), x => x.BrandModel.Contains(input.brandModel));
            obj = obj.WhereIf(!string.IsNullOrWhiteSpace(input.carNum), x => x.CarNum.Contains(input.carNum));
            obj = obj.WhereIf(!string.IsNullOrWhiteSpace(input.company), x => x.Company.Contains(input.company));
            obj = obj.WhereIf(!string.IsNullOrWhiteSpace(input.driverName), x => x.DriverName.Contains(input.driverName));
            var count = obj.Count();
            var result = obj.Skip((input.pageIndex - 1) * input.pageSize).Take(input.pageSize).ToList();
            result = obj.MapToList<Car>();
            return Response<List<Car>>.Ok(result, count);
        }

        public Response<Car> GetCarById(Car input)
        {
            var student = _studentService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = student.MapTo<Car>();
            return Response<Car>.Ok(result);
        }

        public async Task<Response<bool>> PostAddCar(Car input)
        {
            var result = _studentService.InsertAsyncById(input.MapTo<Car>());
            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<bool>> PutUpdateCar(Car input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<Car>());
            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<bool>> CarAdd(Car input)
        {
            var result = _studentService.InsertAsyncById(input.MapTo<Car>());
            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<bool>> CarDelete(Car input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<Car>());
            return Response<bool>.Ok(result > 0);
        }

        public async Task<Response<bool>> CarDeleteDatas(string[] ids)
        {
            var dtos = _studentService.GetListQuery().FirstOrDefault(m => ids.Contains(m.Id.ToString()));
            var result = await _studentService.DeleteAsync(dtos);
            return Response<bool>.Ok(result > 0);
        }

        public async Task<Response<bool>> CarUpdate(Car input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<Car>());
            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<List<Car>>> GetCarAsync()
        {
            var SmartLamps = _studentService.GetListQuery();
            var result = SmartLamps.MapToList<Car>();
            return Response<List<Car>>.Ok(result);
        }

        public Response<Car> GetCarData(Car input)
        {
            var SmartLamps = _studentService.GetListQuery().FirstOrDefault(m => m.Id.ToString() == input.CarNum);
            var result = SmartLamps.MapTo<Car>();
            return Response<Car>.Ok(result);
        }
    }
}
